package com.zeng.controller;


import com.zeng.entity.Result;
import com.zeng.entity.ResultPage;

import com.zeng.entity.Logsinfo;
import com.zeng.service.LogsinfoService;
import com.zeng.utils.TimeUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * (Logsinfo)表控制层
 *
 * @author zengfanchao
 * @since 2021-03-29 14:18:41
 */
@RestController
@RequestMapping("logsinfo")
public class LogsinfoController {
    /**
     * 服务对象
     */
    @Resource
    private LogsinfoService logsinfoService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Logsinfo selectOne(Integer id) {
        return this.logsinfoService.queryById(id);
    }

    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("selectAll")
    public List<Logsinfo> selectAll() {
        return this.logsinfoService.selectAll();
    }

    /**
     * 根据页号查询
     *
     * @param offset 查询初始位置
     * @param limit  查询数量
     * @return
     */
    @GetMapping("selectAllByLimit")
    public ResultPage selectAllByLimit(int offset, int limit) {
        int pageTotal = this.logsinfoService.queryCount();
        List<Logsinfo> list = this.logsinfoService.queryAllByLimit(offset, limit);
        return ResultPage.success(pageTotal, "查询成功", list);
    }

    @PostMapping("selectKeyByLimit")
    public ResultPage selectKeyByLimit(@RequestBody HashMap<String, String> map) {
        if (map.get("key").equals("")) {
            int pageTotal = this.logsinfoService.queryCount();
            List<Logsinfo> list = this.logsinfoService.queryAllByLimit(Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        } else {
            int pageTotal = this.logsinfoService.queryCountSelect(map.get("key"));
            List<Logsinfo> list = this.logsinfoService.queryKeyByLimit(map.get("key"), Integer.valueOf(map.get("offset")), Integer.valueOf(map.get("limit")));
            return ResultPage.success(pageTotal, "查询成功", list);
        }
    }

//    /**
//     * 根据主键删除
//     *
//     * @return
//     */
//    @GetMapping("deleteById")
//    public Result deleteById(Integer id) {
//        this.logsinfoService.deleteById(id);
//        return Result.success("删除成功");
//    }
//
//    @GetMapping("deleteByFlag")
//    public Result deleteByFlag(Integer id) {
//        Logsinfo logsinfo = logsinfoService.queryById(id);
//        logsinfo.setDeleteflag(1);
//        logsinfoService.update(logsinfo);
//        return Result.success("删除成功");
//    }

    @RequestMapping("edit")
    public Result edit(@RequestBody HashMap<String, String> map
    ) {
        Logsinfo logsinfo = this.logsinfoService.queryById(Integer.valueOf(map.get("logid")));
        logsinfo.setOperator(map.get("operator"));
        logsinfo.setCreatetime(TimeUtils.NowTimeN());
        logsinfo.setCreateip(map.get("createip"));
        logsinfo.setDetails(map.get("details"));
        logsinfo.setNote(map.get("note"));
        logsinfoService.update(logsinfo);
        return Result.success("修改成功");
    }

    @RequestMapping("add")
    public Result add(@RequestBody HashMap<String, String> map
    ) {
        Logsinfo logsinfo = new Logsinfo();
        logsinfo.setOperator(map.get("operator"));
        logsinfo.setCreatetime(TimeUtils.NowTimeN());
        logsinfo.setCreateip(map.get("createip"));
        logsinfo.setDetails(map.get("details"));
        logsinfo.setNote(map.get("note"));
        logsinfoService.insert(logsinfo);
        return Result.success("添加成功");
    }
}
